<?php
/**
 *
 *
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
class Index extends Controller
{
    public function _initialize()
    {
        h('phpindexinitialize');
    }
    public function index()
    {
        $c = input('c');
        $this->assign('c', $c);
        if (!empty(cms('tpindex'))) {
            return view(cms('tpindex') . '.html');
        }
        if (file_exists(WEB() . 'index_index.html')) {
            return view();
        } else {
            if (file_exists('app/template/pc/' . MBPC() . '/index_index.html')) {
                return view(pc() . 'index_index.html');
            } else {
                return view('app/template/pc/default_pc/index_index.html'); 
            }
        }
    }
    //频道列表
    public function lists()
    {
        h('phpindexlists');
        $or = input('$or');
        $this->assign('or', $or);
        $alias = input('alias');
        $c = Db::name('cate')->where('alias', $alias)->find();
        $this->assign('c', $c);
        //判断该栏目下是否有二级没有二级跳转列表有二级跳转频道
        $pin = fun_st('cate', array('tid' => $c['id']));
        if (!empty(cms('tplists'))) {
            return view(cms('tplists'));
        }
        if ($pin > 0) {
            //有频道模型
            if (file_exists('app/template/pc/' . MBPC() .  '/'.$c['category_temp'] . '.html')) {
            	$view = $c['category_temp'];
           	} else {
           		$view = 'category_index';
          	}
            $muban = WEB() . $view;
        } else {
            //没有频道模型跳转列表模型
            if (file_exists('app/template/pc/' . MBPC() . '/'. $c['list_temp'] . '.html')) {
            	$view = $c['list_temp'];
           	} else {
           		$view = 'list_index';
          	}
            $muban = WEB() . $view;
        }
        if (empty($c['alias']) || $c['open'] == 0) {
            return $this->error('亲！你迷路了');
        } else {
            if (config('web.WAPOPEN') == 1) {
                if (file_exists($muban . '.html')) {
                    return view($view);
                } else {
                	return view(pc() . $view . '.html');
                }
            } else {
                return view($view);
            }
        }
    }
    //内容页面
    public function html()
    {
        h('phpindexhtml');
        $or = input('or');
        $this->assign('or', $or);
        $id = input('id');
        $model = Db::name('model')->where('name', input('h'))->find();
        if (input('h') != $model['name']) {
            return $this->error('亲！你迷路了');
        }
        $c = Db::name(input('h'))->where('open', 1)->where('id', $id)->find();
        if (empty($c['id'])) {
            return $this->error('亲！你迷路了');
        } else {
            //当model_whole表没有数据给添加记录 对于其他采集导入的数据model_whole表是没有记录的
        	if (empty(fun_st('model_whole',array('tid'=>$id,'model'=>$c['model'])))) {
                Db::name('modelWhole')->data(array('tid'=>$id,'model'=>$c['model'],'uid'=>$c['uid'],'time'=>$c['time'],'open'=>$c['open'],'view'=>$c['view']-1))->insert();
        	}
            Db::name(input('h'))->where('id', $id)->setInc('view', 1);
            Db::name('model_whole')->where('tid', $id)->setInc('view', 1);
            $this->assign('id', $id);
            $this->assign('h', input('h'));
            $this->assign('c', $c);
            if (file_exists('app/template/pc/' . MBPC() . '/'. fun_sc_p('cate', array('model' => $c['model'], 'id' => $c['tid']), 'show_temp') . '.html')) {
            	$view = fun_sc_p('cate', array('model' => $c['model'], 'id' => $c['tid']), 'show_temp');
           	} else {
           		$view = 'show_index';
          	}
            $muban = WEB() . $view;
            //付费开始
            //查询是否付费内容
            if ($c['fufei'] == 1) {
                $fufei = 0;
                //查询是否付费过
                $f = fun_st('fufei', array('tid' => $id, 'model' => $c['model'], 'uid' => session('userid')));
                if ($f >= 1) {
                    //付费过
                    $fufei = 0;
                } else {
                    //没有付费过
                    $fufei = 1;
                }
            } else {
                $fufei = 0;
            }
            $this->assign('fufei', $fufei);
            if (!empty(cms('tphtml'))) {
                return view(cms('tphtml') . '.html');
            }
            if (config('web.WAPOPEN') == 1) {
                if (file_exists($muban . '.html')) {
                    return view($view);
                } else {
                    return view(pc() . $view . '.html');
                }
            } else {
                return view($view);
            }
        }
    }
    //搜索
    public function soso()
    {
        h('phpindexsoso');
        $k = preg_replace("/<|>/s", "", input('ks'));
        $ks = addslashes($k);
        $model = input('model');
        $this->assign('ks', $ks);
        $this->assign('model', $model);
        $m = Db::name('model')->where('name', $model)->find();
        if (empty($ks) || $model != $m['name']) {
            return $this->error('亲！你迷路了');
        } else {
            $view = fun_sc_p('model', array('name' => $model), 'soso');
            $muban = WEB() . $view;
            $c = input('c');
            $this->assign('c', $c);
            if (config('web.WAPOPEN') == 1) {
                if (file_exists($muban . '.html')) {
                    return view($view);
                } else {
                    return view(pc() . $view . '.html');
                }
            } else {
                return view($view);
            }
        }
    }
    
    //TAG
    public function tag()
    {
        h('phpindextag');
        $tag = preg_replace("/<|>/s", "", input('tag'));
        $this->assign('tag', $tag);
        $model = input('model');
        $this->assign('model', $model);
        $m = Db::name('model')->where('name', $model)->find();
        if (empty($tag) || $model != $m['name']) {
            return $this->error('亲！你迷路了');
        } else {
            $view = 'index_tag';
            $muban = WEB() . $view;
            $c = input('c');
            $this->assign('c', $c);
            if (config('web.WAPOPEN') == 1) {
                if (file_exists($muban . '.html')) {
                    return view($view);
                } else {
                    return view(pc() . $view . '.html');
                }
            } else {
                return view($view);
            }
        }
    }
    //个人空间
    public function uid()
    {
        h('phpindexuid');
        $c = Db::name('member')->where('userid', input('uid'))->find();
        $this->assign('c', $c);
        if ($c['open'] == 0) {
            $this->error('当前账号异常', url('/'));
        }
        if (!empty(cms('tpuid'))) {
            return view(cms('tpuid') . '.html');
        }
        if (file_exists(WEB() . 'index_uid.html')) {
            return view();
        } else {
            if (file_exists('app/template/pc/' . MBPC() . '/index_uid.html')) {
                return view(pc() . 'index_uid.html');
            } else {
                return view('app/template/pc/default_pc/index_uid.html');
            }
        }
    }
    //单页
    public function danye()
    {
        h('phpindexdanye');
        $show = input('show');
        $c = Db::name('danye')->where('show', $show)->find();
        $this->assign('c', $c);
        $muban = WEB() . $c['name'];
        if (empty($c['show']) || $c['open'] == 0) {
            return $this->error('亲！你迷路了');
        } else {
            if (config('web.WAPOPEN') == 1) {
                if (file_exists($muban)) {
                    return view($muban);
                } else {
                    return view(pc() . $c['name']);
                }
            } else {
                if (file_exists('app/template/pc/' . MBPC() . '/' . $c['name'])) {
                    return view('app/template/pc/' . MBPC() . '/' . $c['name']);
                } else {
                    return view($muban);
                }
            }
        }
    }
    //找回密码
    public function retrieve()
    {
    	h('phpindexretrieve');
        if (session('usermail') || session('userid')) {
            $this->error('当前处于登录状', '/');
        }
        if (!empty(cms('tpretrieve'))) {
            return view(cms('tpretrieve') . '.html');
        }
        if (file_exists(WEB() . 'index_retrieve.html')) {
            return view();
        } else {
            if (file_exists('app/template/pc/' . MBPC() . '/index_retrieve.html')) {
               return view(pc() . 'index_retrieve.html');
            } else {
                return view('app/template/pc/default_pc/index_retrieve.html');
            }
        }
    }
    //注册
    public function reg()
    {
    	h('phpindexreg');
        if (session('usermail') || session('userid')) {
            $this->error('当前处于登录状', '/');
        }
        if (!empty(cms('tpreg'))) {
            return view(cms('tpreg') . '.html');
        }
        if (file_exists(WEB() . 'index_reg.html')) {
            return view();
        } else {
            if (file_exists('app/template/pc/' . MBPC() . '/index_reg.html')) {
                return view(pc() . 'index_reg.html');
            } else {
                return view('app/template/pc/default_pc/index_reg.html');
            }
        }
    }
    //登录
    public function login()
    {
    	h('phpindexlogin');
        if (session('usermail') || session('userid')) {
            $this->error('当前处于登录状', '/');
        }
        if (!empty(cms('tplogin'))) {
            return view(cms('tplogin') . '.html');
        }
        if (file_exists(WEB() . 'index_login.html')) {
            return view();
        } else {
            if (file_exists('app/template/pc/' . MBPC() . '/index_login.html')) {
                return view(pc() . 'index_login.html');
            } else {
                return view('app/template/pc/default_pc/index_login.html');
            }
        }
    }
    //pc设置
    public function cmspc()
    {
    	h('phpindexcmspc');
        if (!session('admin_name')) {
            $this->error('请登录', url('admin/login/index'));
        } else {
            if (request()->isPost()) {
                $url = 'app/template/pc/' . cms('default') . '/Cms_pc.php';
                $info = (include $url);
                $config = array();
                $config = $this->request->post();
                $config = preg_replace("/\[|\]|'|\<\?php|\?\>|embed|data:|iframe|code/i", '', $config);
                $res = array_merge($info, $config);
                $str = '<?php return [';
                foreach ($res as $key => $value) {
                    $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
                }
                $str .= ']; ';
                if (file_put_contents($url, $str)) {
                    return json(array('code' => 200, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
            return view('app/template/pc/' . MBPC() . '/Cms_pc.html');
        }
    }
    //wap设置
    public function cmswap()
    {
    	h('phpindexcmswap');
        if (!session('admin_name')) {
            $this->error('请登录', url('admin/login/index'));
        } else {
            if (request()->isPost()) {
                $url = 'app/template/wap/' . cms('defaultwap') . '/Cms_wap.php';
                $info = (include $url);
                $config = array();
                $config = $this->request->post();
                $config = preg_replace("/\[|\]|'|\<\?php|\?\>|embed|data:|iframe|code/i", '', $config);
                $res = array_merge($info, $config);
                $str = '<?php return [';
                foreach ($res as $key => $value) {
                    $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
                }
                $str .= ']; ';
                if (file_put_contents($url, $str)) {
                    return json(array('code' => 200, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
            return view('app/template/wap/' . MBWAP() . '/Cms_wap.html');
        }
    }
    public function testing()
    {
    	return 1;
    }
}